package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.VideoModelService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.VideoModel;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Description: 视频规则
 * @author: Rock
 * @time: 2020/8/25 11:20
 */
@Controller
@RequestMapping("videoModel")
public class VideoModelController extends BaseController {

    @Resource
    private VideoModelService videoModelService;

    /**
     * @param [page, vender]
     * @Description: 设备型号管理
     * @time: 2020/8/22 14:47
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<VideoModel> pages = videoModelService.list(page);
        mv.addObject("videos", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("videoModel/video_model_list");
        return mv;
    }

    /**
     * @param id
     * @param name
     * @time: 2020/10/29 14:08
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("checkName")
    public Response<Object> checkName(@RequestParam(required = false) String id, String name) {
        VideoModel videoModel = videoModelService.getByName(name);
        if (StringUtil.isBlank(id)) {
            if (videoModel != null) {
                return Response.failedResponse("名称已存在");
            }
        } else {
            if (videoModel != null && !id.equals(videoModel.getId())) {
                return Response.failedResponse("名称已存在");
            }
        }
        return Response.successResponse();
    }

    /**
     * @Description: 添加页面
     * @time: 2020/8/17 17:21
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("videoModel/video_model_add");
        return mv;
    }

    /**
     * @Description: 保存
     * @time: 2020/8/17 17:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(VideoModel video) {
        ModelAndView mv = new ModelAndView();
        String msg = "success";
        if (videoModelService.getByName(video.getName()) == null) {
            SysUser user = this.getSessionUser();
            if (user != null) {
                video.setCreater(user.getAccount());
                videoModelService.save(video, user, this.getRemortIP());
            }
        } else {
            msg = "该视频规则名称已存在";
        }
        mv.addObject("msg", msg);
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @Description: 编辑页面
     * @time: 2020/8/17 17:21
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("video", videoModelService.getById(id));
        mv.setViewName("videoModel/video_model_edit");
        return mv;
    }

    /**
     * @Description: 保存
     * @time: 2020/8/17 17:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(VideoModel video) {
        ModelAndView mv = new ModelAndView();
        String msg = videoModelService.update(video, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", msg);
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param [id]
     * @Description: 删除
     * @time: 2020/8/22 15:17
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("del")
    public Response<Object> del(String id) {
        String msg = videoModelService.del(id, this.getSessionUser(), this.getRemortIP());
        if (StringUtils.isEmpty(msg)) {
            return Response.successResponse();
        } else {
            return Response.failedResponse(msg);
        }
    }

}
